package com.z.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.db4o.ObjectContainer;
import com.db4o.query.Predicate;
import com.z.base.DbBase;
import com.z.base.Page;
import com.z.entity.UploadFile;

public class UploadFileDao extends DbBase {
	private static UploadFileDao uploadFileDao;

	public static UploadFileDao getInstace() {
		if (uploadFileDao == null) {
			uploadFileDao = new UploadFileDao();
		}
		return uploadFileDao;
	}

	@SuppressWarnings("serial")
	public UploadFile get(final String id) {
		ObjectContainer db = getDB();
		List<UploadFile> all = db.query(new Predicate<UploadFile>() {
			@Override
			public boolean match(UploadFile uploadFile) {
				return uploadFile.getId().equals(id);
			}
		});
		if (all != null && all.size() > 0) {
			return all.get(0);
		}
		return null;
	}

	@SuppressWarnings("serial")
	public void finish(final String id, String swfPath) {
		ObjectContainer db = getDB();
		List<UploadFile> all = db.query(new Predicate<UploadFile>() {
			@Override
			public boolean match(UploadFile uploadFile) {
				return uploadFile.getId().equals(id);
			}
		});
		if (all != null && all.size() > 0) {
			UploadFile uploadFile = all.get(0);
			uploadFile.setReadyTime(new Date(System.currentTimeMillis()));
			uploadFile.setSwfPath(swfPath);
			db.store(uploadFile);
			db.commit();
		}
	}

	@SuppressWarnings("serial")
	public void find(final String param, final List<String> idList, Page page) {
		List<UploadFile> list = new ArrayList<UploadFile>();
		ObjectContainer db = getDB();
		List<UploadFile> all = db.query(new Predicate<UploadFile>() {
			@Override
			public boolean match(UploadFile uploadFile) {
				if (uploadFile.getReadyTime() == null) {
					return false;
				}
				if (param == null || param.length() == 0) {
					return true;
				}
				return uploadFile.getName().contains(param)
						|| idList.contains(uploadFile.getId())
						|| uploadFile.getSummary().contains(param);
			}
		});
		if (all != null && all.size() > 0) {
			page.setTotalCount(all.size());
			page.computeTotalPages();
			int begin = page.getPageSize() * (page.getPageNo() - 1);
			for (int i = begin; (i < begin + page.getPageSize())&&(i<all.size()); i++) {
				list.add(all.get(i));
			}
		}
		page.setList(list);
	}

	@SuppressWarnings("serial")
	public void delete(final String id) {
		ObjectContainer db = getDB();
		List<UploadFile> list = db.query(new Predicate<UploadFile>() {
			@Override
			public boolean match(UploadFile uploadFile) {
				return uploadFile.getId().equals(id);
			}
		});
		if (list.size() > 0) {
			db.delete(list.get(0));
		}
	}

	/**
	 * 检查指定的MD5的文件是否存在
	 * 
	 * @param md5
	 * @return
	 */
	@SuppressWarnings("serial")
	public boolean fileExistedByMD5(final String md5) {
		ObjectContainer db = null;
		db = getDB();
		List<UploadFile> list = db.query(new Predicate<UploadFile>() {
			@Override
			public boolean match(UploadFile uploadFile) {
				return uploadFile.getId().equals(md5);
			}
		});
		if (list.size() > 0) {
			return true;
		}
		return false;
	}
}
